package com.zld.smarket.controller;

import com.zld.smarket.pojo.Goods;
import com.zld.smarket.result.ExceptionMsg;
import com.zld.smarket.result.ResponseData;
import com.zld.smarket.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public ResponseData findList(Integer page, Integer limit){
        return  goodsService.findList(page,limit);
    }

    @RequestMapping(value = "/add")
    public ResponseData add(Goods goods){
        Long i=goods.getGoodsNo();
        if(goodsService.selectbyno(i)>0){
            return new ResponseData(ExceptionMsg.ADDERROR);
        }else {

            Integer integer = goodsService.addGoods(goods);
            return new ResponseData(ExceptionMsg.SUCCESS);
        }
    }
    @RequestMapping(value = "/del")
    public ResponseData delete(Long[] ids){
        Integer integer = goodsService.deleteById(ids);
        return new ResponseData(ExceptionMsg.SUCCESS);
    }


    @RequestMapping(value = "/search",method = RequestMethod.GET)
    public ResponseData search(String s,Integer page, Integer limit){
        return  goodsService.search(s,page,limit);
    }

    @RequestMapping(value = "/update")
    public ResponseData updateById(Goods goods){
        return goodsService.updateById(goods);
    }


    @RequestMapping("/getName")
    public List<Goods> getName() {
        return goodsService.selectAll();
    }

    @RequestMapping(value = "/up",method = RequestMethod.POST)
    public ResponseData upimg(MultipartFile file, HttpServletRequest request){
        return goodsService.upimg(file,request);

    }
}
